package com.loveyukari.Interceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.loveyukari.service.ManagerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.FlashMap;
import org.springframework.web.servlet.FlashMapManager;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;


/**
*
*yangshaohua
*2016年9月2日上午10:34:37
**/
@Component
public class LoginInterceptor implements HandlerInterceptor{
	
	@Autowired
	private ManagerService managerService;

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		String sign =(String) request.getAttribute("sign");
		if (managerService.getManageAuthBySign(sign) != null){
			return true;
		}
		FlashMap flashMap = new FlashMap();
		flashMap.put("message", "未登录或登录过期");
		flashMap.put("uri", request.getRequestURI());
		FlashMapManager flashMapManager = RequestContextUtils.getFlashMapManager(request);
		flashMapManager.saveOutputFlashMap(flashMap, request, response);
		response.sendRedirect(request.getContextPath()+"/login/index");
		return false;
	}
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		String sign = request.getAttribute("sign").toString();
		int lv = managerService.getManageAuthority(sign);
		if (modelAndView != null){
			modelAndView.getModel().put("manageAuthLv",lv);
		}
		// TODO Auto-generated method stub
		
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
	

}
